import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.ButtonGroup;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import java.awt.Color;
import java.awt.GridBagLayout;

import javax.swing.JLabel;

import java.awt.GridBagConstraints;
import java.awt.Font;

import javax.swing.JComboBox;

import java.awt.Insets;

import javax.swing.JToggleButton;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowListener;
import java.io.File;

import javax.swing.JButton;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;


public class SettingsFrame extends JFrame {

	private JPanel contentPane;
	private Option gameOption;
	
	//Button
	JLabel label;
	JToggleButton tglBtnEasy = null ;
	JToggleButton tglBtnMedium = null;
    JToggleButton tglBtnHard = null;
	private JLabel labelSfondo = null;
	private JButton btnFileChooser = null;
	private JLabel labelComand = null;
	private JLabel labelUP = null;
	private JLabel labelDown = null;
	private JLabel label_1 = null;
	private JToggleButton btnItalian = null;
	private JToggleButton btnEnglish = null;
	private JToggleButton btnGerman = null;
	
	/**
	 * Create the frame.
	 */
	public SettingsFrame(Option gameOpt){
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent arg0) {
				
				gameOption.getMenu().setEnabled(true);
//				gameOption.getMenu().init();
			}
		});
		this.gameOption = gameOpt;
		gameOption.getMenu().setEnabled(false);
		
		
		
		
		// METODO INIT !!! 
		init();
		
		ButtonGroup diffGroup = new ButtonGroup();
		diffGroup.add(tglBtnEasy);
		diffGroup.add(tglBtnMedium);
		diffGroup.add(tglBtnHard);
		
		
	}
	
	protected void init() {
		setBounds(100, 100, 865, 529);
		contentPane = new JPanel();
		contentPane.setBackground(Color.BLACK);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		label = new JLabel(gameOption.getResource().getString("difficulty"));
		label.setForeground(new Color(0, 191, 255));
		label.setFont(new Font("Lucida Console", Font.PLAIN, 23));
		label.setBounds(34, 50, 226, 24);
		contentPane.add(label);
		
		tglBtnEasy = new JToggleButton(gameOption.getResource().getString("easy"));
		tglBtnEasy.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (tglBtnEasy.isSelected()) {
					gameOption.setDifficolta(gameOption.EASY);
					System.out.println("Difficolta settata a facile.");
				}
			}
		});
		
		
		tglBtnEasy.setForeground(new Color(0, 191, 255));
		tglBtnEasy.setFont(new Font("Lucida Console", Font.PLAIN, 16));
		tglBtnEasy.setBounds(71, 88, 94, 29);
		contentPane.add(tglBtnEasy);
		
		
		
		tglBtnMedium = new JToggleButton(gameOption.getResource().getString("medium"));
		tglBtnMedium.setSelected(true);
		tglBtnMedium.setForeground(new Color(0, 191, 255));
		tglBtnMedium.setFont(new Font("Lucida Console", Font.PLAIN, 16));
		tglBtnMedium.setBounds(171, 88, 95, 29);
		contentPane.add(tglBtnMedium);
		
		tglBtnMedium.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (tglBtnMedium.isSelected()) {
					gameOption.setDifficolta(gameOption.MEDIUM);
					System.out.println("Difficolta settata a Normale.");
				}
			}
		});
		
		tglBtnHard = new JToggleButton(gameOption.getResource().getString("hard"));
		tglBtnHard.setForeground(new Color(0, 191, 255));
		tglBtnHard.setFont(new Font("Lucida Console", Font.PLAIN, 16));
		tglBtnHard.setBounds(272, 86, 134, 32);
		contentPane.add(tglBtnHard);
		
		tglBtnHard.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (tglBtnHard.isSelected()) {
					gameOption.setDifficolta(gameOption.HARD);
					System.out.println("Difficolta settata a Difficile.");
				}
			}
		});
		
		
		labelSfondo = new JLabel(gameOption.getResource().getString("bg"));
		labelSfondo.setForeground(new Color(0, 191, 255));
		labelSfondo.setFont(new Font("Lucida Console", Font.PLAIN, 23));
		labelSfondo.setBounds(34, 154, 303, 24);
		contentPane.add(labelSfondo);
		
		btnFileChooser = new JButton(gameOption.getResource().getString("choose"));
		btnFileChooser.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				JFileChooser fileChooser = new JFileChooser();
				fileChooser.setDialogTitle(gameOption.getResource().getString("chooseBG"));
				
				fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
				int val = fileChooser.showOpenDialog(gameOption.getMenu());
				File x = fileChooser.getSelectedFile();
				if(val == JFileChooser.APPROVE_OPTION)
				if(x.getName().endsWith(".png") || x.getName().endsWith(".jpg")) {
					System.out.println("NOME FILE SELEZIONATO : "+x.getName());
					btnFileChooser.setText(x.getName());
					System.out.println("PATH DEL FILE SELEZIONATO : "+x.getPath());
					gameOption.setBackGroundPath(x.getPath());
				} else {
					
					System.out.println("FILE SELEZIONATO NON VALIDO");
					
				}
				
			}
		});
		btnFileChooser.setForeground(new Color(0, 191, 255));
		btnFileChooser.setFont(new Font("Lucida Console", Font.PLAIN, 16));
		btnFileChooser.setBounds(71, 210, 189, 29);
		contentPane.add(btnFileChooser);
		
		labelComand = new JLabel(gameOption.getResource().getString("commands"));
		labelComand.setForeground(new Color(0, 191, 255));
		labelComand.setFont(new Font("Lucida Console", Font.PLAIN, 23));
		labelComand.setBounds(34, 279, 303, 24);
		contentPane.add(labelComand);
		
		JLabel lblW = new JLabel("W");
		lblW.setForeground(new Color(0, 191, 255));
		lblW.setFont(new Font("Lucida Console", Font.PLAIN, 23));
		lblW.setBounds(108, 332, 42, 24);
		contentPane.add(lblW);
		
		JLabel lblS = new JLabel("S");
		lblS.setForeground(new Color(0, 191, 255));
		lblS.setFont(new Font("Lucida Console", Font.PLAIN, 23));
		lblS.setBounds(106, 384, 32, 24);
		contentPane.add(lblS);
		
		labelUP = new JLabel(gameOption.getResource().getString("up"));
		labelUP.setForeground(new Color(0, 191, 255));
		labelUP.setFont(new Font("Lucida Console", Font.PLAIN, 23));
		labelUP.setBounds(218, 332, 167, 24);
		contentPane.add(labelUP);
		
		labelDown = new JLabel(gameOption.getResource().getString("down"));
		labelDown.setForeground(new Color(0, 191, 255));
		labelDown.setFont(new Font("Lucida Console", Font.PLAIN, 23));
		labelDown.setBounds(218, 384, 177, 24);
		contentPane.add(labelDown);
		
		label_1 = new JLabel(gameOption.getResource().getString("lan"));
		label_1.setForeground(new Color(0, 191, 255));
		label_1.setFont(new Font("Lucida Console", Font.PLAIN, 23));
		label_1.setBounds(466, 279, 283, 24);
		contentPane.add(label_1);
		
		btnItalian = new JToggleButton(gameOption.getResource().getString("it"));
		btnItalian.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				gameOption.setLanguage("it");
				refreshComponent();
				gameOption.getMenu().refreshComponent();
				
				
			}
		});
		btnItalian.setForeground(new Color(0, 191, 255));
		btnItalian.setFont(new Font("Lucida Console", Font.PLAIN, 16));
		btnItalian.setBounds(510, 333, 168, 29);
		contentPane.add(btnItalian);
		
		btnEnglish = new JToggleButton(gameOption.getResource().getString("eng"));
		btnEnglish.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				gameOption.setLanguage("en");
				refreshComponent();
				gameOption.getMenu().refreshComponent();
				
			}
		});
		
		ButtonGroup lanBtnGroup = new ButtonGroup();
		lanBtnGroup.add(btnEnglish);
		lanBtnGroup.add(btnItalian);
		
		btnEnglish.setForeground(new Color(0, 191, 255));
		btnEnglish.setFont(new Font("Lucida Console", Font.PLAIN, 16));
		btnEnglish.setBounds(510, 379, 168, 29);
		contentPane.add(btnEnglish);
		
		btnGerman = new JToggleButton(gameOption.getResource().getString("ger"));
		btnGerman.setForeground(new Color(0, 191, 255));
		btnGerman.setFont(new Font("Lucida Console", Font.PLAIN, 16));
		btnGerman.setBounds(510, 420, 168, 29);
		contentPane.add(btnGerman);
		lanBtnGroup.add(btnGerman);
		
		btnGerman.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				gameOption.setLanguage("de");
				refreshComponent();
				gameOption.getMenu().refreshComponent();
				
			}
		});
		
	}
	
	public void refreshComponent() {
		
		label.setText(gameOption.getResource().getString("difficulty"));
		tglBtnEasy.setText(gameOption.getResource().getString("easy"));
		tglBtnMedium.setText(gameOption.getResource().getString("medium"));
		tglBtnHard.setText(gameOption.getResource().getString("hard"));
		labelSfondo.setText(gameOption.getResource().getString("bg"));
		btnFileChooser.setText(gameOption.getResource().getString("choose"));
		labelComand.setText(gameOption.getResource().getString("commands"));
		labelUP.setText(gameOption.getResource().getString("up"));
		labelDown.setText(gameOption.getResource().getString("down"));
		label_1.setText(gameOption.getResource().getString("lan"));
		btnItalian.setText(gameOption.getResource().getString("it"));
		btnEnglish.setText(gameOption.getResource().getString("eng"));
		btnGerman.setText(gameOption.getResource().getString("ger"));
		
		
		
	}
	
	
	
}
